home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / NetTimeProtocol ƒ / ntp.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-23  |  8.5 KB  |  319 lines  |  [TEXT/MPS ]

  1. #ifndef lint
  2. static char *RCSntp = "$Header: ntp.h,v 2.7 89/02/19 16:08:33 bww Exp $";
  3. #endif
  4.  
  5. /*
  6.  ****************************************************************
  7.  * HISTORY
  8.  * $Log:    ntp.h,v $
  9.  * Revision 2.7  89/02/19  16:08:33  bww
  10.  *     SSP headers.
  11.  *     [89/02/19  16:06:13  bww]
  12.  * 
  13.  * Revision 2.6  88/11/11  16:06:58  bww
  14.  * Upgraded to latest UMD release.
  15.  * 
  16.  * Revision 2.5  88/09/20  21:59:13  bww
  17.  * Added support for hashed peer list.
  18.  * 
  19.  * Revision 2.4  88/08/30  18:29:41  bww
  20.  * Let's try a 256ms SET_THRESHOLD.
  21.  * 
  22.  * Revision 2.3  88/08/20  19:07:20  bww
  23.  * Allowance for flaky UNIX time on Suns.
  24.  * 
  25.  * Revision 2.2  88/07/28  18:42:31  bww
  26.  * First CMUCS release
  27.  * 
  28.  * Revision 2.1  88/04/25  18:00:36  root
  29.  * Initial CMU revision
  30.  * 
  31.  * Revision 2.1  88/04/25  18:00:36  root
  32.  * *** empty log message ***
  33.  * 
  34.  * Revision 2.0  88/03/31  10:25:06  root
  35.  * *** empty log message ***
  36.  * 
  37.  * Revision 1.2  88/02/28  22:59:25  petry
  38.  * *** empty log message ***
  39.  * 
  40.  * Revision 1.1  87/12/16  10:33:07  root
  41.  * Initial revision
  42.  * 
  43.  */
  44.  
  45. /* b_32 from MacTCP commontypes */
  46. #define u_long b_32 
  47. #define u_short b_16
  48. #define u_char b_8
  49. #define u_int b_16
  50.  
  51. #define htonl(a) (a)
  52. #define ntohl(a) (a)
  53. #define ntohs(a) (a)
  54.  
  55. /* the clock in the mac is in seconds since
  56.  * Jan 1 1904.
  57.  * timeval's are expected on the mac to hold
  58.  * the time in seconds + microseconds of
  59.  * GMT since Jan 1, 1970.
  60.  */ 
  61.  
  62. struct timeval {
  63.     time_t tv_sec;
  64.     time_t tv_usec;
  65. };
  66.  
  67. struct timezone {
  68.     int tz_minuteswest;
  69.     int    tz_dsttime;
  70. };
  71.  
  72. struct intf {
  73.     int fd;
  74.     char *name;
  75.     StreamPtr sin;
  76.     StreamPtr bcast;
  77.     int uses;
  78.     int if_flags;
  79. };
  80.  
  81. /*
  82.  *  Definitions for the masses
  83.  */
  84. #define    MAXNAMELENGTH    80
  85. #define    JAN_1970    2208988800    /* 1970 - 1900 in seconds */
  86. #define    HIGH_BIT    0x80000000    /* sign bit on fixed point */
  87.  
  88. /*
  89.  *  User program specific (ntp.c)
  90.  */
  91. #define RETRY_COUNT    2    /* number of times we want to retry */
  92. #define TIME_OUT        60    /* time to wait for reply, in secs */
  93.  
  94. /*
  95.  *  Daemon specific (ntpd.c)
  96.  */
  97. #define SAMPLES        8    /* number of samples for 1st order filter */
  98. #define    SHIFT_MASK    0xff    /* number of intervals to wait */
  99. #define    WAYTOOBIG    1000.0    /* Too many seconds to correct, something is
  100.                  * really wrong */
  101. #define    SET_THRESHOLD    0.128    /* Threshold for using settimeofday */
  102.  
  103. #define    NTPINITFILE    "ntp.conf"
  104.  
  105. /*
  106.  *  Definitions outlined in the NTP spec
  107.  */
  108. #define    NTP_VERSION    1
  109. #define    NTP_PORT    123    /* for ref only (see /etc/services) */
  110. #define    NTP_MINPOLL    6    /* (64) seconds between messages */
  111. #define    NTP_MAXPOLL    10    /* (1024) secs to poll */
  112. #define    NTP_GRANPOLL    5    /* Granularity of poll intervals in seconds */
  113. #define    NTP_MAXDISP    65.5360    /* Maximum dispersion */
  114. #define    PEER_SHIFT    8    /* size of shift register */
  115. #define    PEER_THRESHOLD    .500    /* dispersion threshold */
  116. #define    PEER_FILTER    .5    /* filter weight */
  117. #define    PEER_SELECT    .75    /* select weight */
  118.  
  119. #define    NTP_MAXDELAY    65536.0    /* Maximum dispersion seconds*/
  120.  
  121. /*
  122.  * Structure definitions for NTP fixed point values
  123.  *
  124.  *    0                   1                   2                   3
  125.  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  126.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  127.  *   |                         Integer Part                          |
  128.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  129.  *   |                         Fraction Part                         |
  130.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  131.  *
  132.  *    0                   1                   2                   3
  133.  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  134.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  135.  *   |            Integer Part       |     Fraction Part             |
  136.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  137. */
  138. struct l_fixedpt {
  139.     u_long int_part;
  140.     u_long fraction;
  141. };
  142.  
  143. struct s_fixedpt {
  144.     u_short sint_part;
  145.     u_short sfraction;
  146. };
  147. /*
  148.  *    NTP packet definitions
  149.  *
  150.  *    0                   1                   2                   3
  151.  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  152.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  153.  *   |LI | VN  |0 0 0|    Stratum    |      Poll     |   Precision   |
  154.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  155.  *   |                     Synchronizing Distance                    |
  156.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  157.  *   |                     Estimated Drift Rate                      |
  158.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  159.  *   |                  Reference Clock Identifier                   |
  160.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  161.  *   |                                                               |
  162.  *   |                 Reference Timestamp (64 bits)                 |
  163.  *   |                                                               |
  164.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  165.  *   |                                                               |
  166.  *   |                 Originate Timestamp (64 bits)                 |
  167.  *   |                                                               |
  168.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  169.  *   |                                                               |
  170.  *   |                  Receive Timestamp (64 bits)                  |
  171.  *   |                                                               |
  172.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  173.  *   |                                                               |
  174.  *   |                  Transmit Timestamp (64 bits)                 |
  175.  *   |                                                               |
  176.  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  177. */
  178. struct ntpdata {
  179.     u_char status;        /* status of local clock and leap info */
  180.     u_char stratum;        /* Stratum level */
  181.     u_char poll;        /* poll value */
  182.     char precision;
  183.     struct s_fixedpt distance;
  184.     u_long drift;
  185.     u_long refid;
  186.     struct l_fixedpt reftime;
  187.     struct l_fixedpt org;
  188.     struct l_fixedpt rec;
  189.     struct l_fixedpt xmt;
  190. };
  191. /*
  192.  *    Leap Second Codes (high order two bits)
  193.  */
  194. #define    NO_WARNING    0x00    /* no warning */
  195. #define    PLUS_SEC    0x40    /* add a second (61 seconds) */
  196. #define    MINUS_SEC    0x80    /* minus a second (59 seconds) */
  197. #define    ALARM        0xc0    /* alarm condition (clock unsynchronized) */
  198.  
  199. /*
  200.  *    Clock Status Bits that Encode Version
  201.  */
  202. #define    NTPVERSION_1    0x08
  203. #define    VERSIONMASK    0x38
  204. #define LEAPMASK    0xc0
  205.  
  206. /*
  207.  *    Stratum Definitions
  208.  */
  209. #define    UNSPECIFIED    0
  210. #define    PRIM_REF    1    /* radio clock */
  211. #define    MAXSTRATUM    60
  212. #define    INFO_QUERY    62    /* **** THIS implementation dependent **** */
  213. #define    INFO_REPLY    63    /* **** THIS implementation dependent **** */
  214.  
  215. #define    ACTIVE        0x01    /* opposite of PASSIVE */
  216. #define MEDIAN        0x02
  217.  
  218. /*
  219.  *  Structure of the per peer variables
  220.  */
  221. struct ntp_peer {
  222.     struct ntp_peer *next_peer;
  223.     struct ntp_peer *next_hash;
  224.     ip_addr src;
  225.     ip_port port;
  226.     int sock;
  227.     u_long state;
  228.     u_long reach;
  229.     u_long timer;
  230.     u_long threshold;
  231.     u_char leap;
  232.     u_char stratum;
  233.     u_char ppoll;
  234.     u_char hpoll;
  235.  
  236.     struct s_fixedpt distance;
  237.     char precision;
  238.     u_long drift;
  239.     u_long refid;
  240.     struct l_fixedpt reftime;
  241.     /*
  242.      * first order offsets
  243.      */
  244.     struct filter {
  245.         short index;
  246.         double offset[SAMPLES];
  247.         double delay[SAMPLES];
  248.     } filter;
  249.     double delay;
  250.     double offset;
  251.     double dispersion;
  252.  
  253.     struct l_fixedpt rec;
  254.     struct l_fixedpt org;
  255.     u_long pkt_sent;
  256.     u_long pkt_rcvd;
  257. };
  258.  
  259. struct select_list {
  260.     struct ntp_peer *peer;
  261.     u_short sort_item;
  262.     double dsp;
  263. };
  264.  
  265. /*
  266.  *  These structures are used to pass information to the ntpdc (control)
  267.  *  program.  They are unique to this implementation and not part of the
  268.  *  NTP specification.
  269.  */
  270. struct clockinfo {
  271.     u_long net_address;
  272.     u_long my_address;
  273.     u_short port;
  274.     u_short flags;
  275.     u_long pkt_sent;
  276.     u_long pkt_rcvd;
  277.     u_long timer;
  278.     long threshold;
  279.  
  280.     u_char leap;
  281.     u_char stratum;
  282.     u_char ppoll;
  283.     char precision;
  284.  
  285.     u_char hpoll;
  286.     u_char reach;
  287.     u_short filler;
  288.  
  289.     u_long drift;
  290.     u_long refid;
  291.     struct l_fixedpt reftime;
  292.     struct info_filter {
  293.         short index;
  294.         short filler;
  295.         struct l_fixedpt offset[SAMPLES];
  296.         struct l_fixedpt delay[SAMPLES];
  297.     } info_filter;
  298. };
  299.  
  300. struct ntpinfo {
  301.     char filler;
  302.     u_char type;        /* request type (stratum in ntp packets) */
  303.     u_short count;        /* pad to 32-bit boundary */
  304.     struct clockinfo clocks[1];
  305. };
  306.  
  307. struct sysdata {
  308.     struct s_fixedpt distance;
  309.     u_long drift;
  310.     u_long refid;
  311.     struct l_fixedpt reftime;
  312.     struct ntp_peer *peer;
  313.     int maxpeers;
  314.     u_char leap;
  315.     u_char stratum;
  316.     char precision;
  317.     u_char filler;        /* put here for %&*%$$ SUNs */
  318. };
  319.